import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import { LoginData, LoginResult, VerifyCodeResult, TenantInfo, WebSiteInfo } from './types';
import { UserInfo } from '@/api/system/user/types';

const isDev = import.meta.env.MODE === 'development';

// pc端固定客户端授权id
const clientId = import.meta.env.VITE_APP_CLIENT_ID;

/**
 * @param data {LoginData}
 * @returns
 */
export function login(data: LoginData): AxiosPromise<LoginResult> {
  const params = {
    ...data,
    clientId: data.clientId || clientId,
    grantType: data.grantType || 'password'
  };

  return request({
    url: '/auth/login',
    headers: {
      isToken: false,
      isEncrypt: true
    },
    method: 'post',
    data: params
  });
}

// 注册方法
export function register(data: any) {
  const params = {
    ...data,
    clientId: clientId,
    grantType: 'password'
  };
  return request({
    url: '/auth/register',
    headers: {
      isToken: false,
      isEncrypt: true
    },
    method: 'post',
    data: params
  });
}

/**
 * 注销
 */
export function logout() {
  return request({
    url: '/auth/logout',
    method: 'post'
  });
}

/**
 * 获取验证码
 */
export function getCodeImg(): AxiosPromise<VerifyCodeResult> {
  return request({
    url: '/auth/code',
    headers: {
      isToken: false
    },
    method: 'get',
    timeout: 20000
  });
}

/**
 * 第三方登录
 */
export function callback(data: LoginData): AxiosPromise<any> {
  const LoginData = {
    ...data,
    clientId: clientId,
    grantType: 'social'
  };
  return request({
    url: '/auth/social/callback',
    method: 'post',
    data: LoginData
  });
}

// 获取用户详细信息
export function getInfo(): AxiosPromise<UserInfo> {
  return request({
    url: '/system/user/getInfo',
    method: 'get'
  });
}

// 获取租户列表
export function getTenantList(): AxiosPromise<TenantInfo> {
  return request({
    url: '/auth/tenant/list',
    headers: {
      isToken: false
    },
    method: 'get'
  });
}

// 获取网站初始信息
export function queryByDomain(data: { domain: string }): AxiosPromise<WebSiteInfo> | any {
  return request({
    url: '/system/unitDomain/queryByDomain',
    headers: {
      isToken: false,
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    method: 'post',
    data
  });
}

export function getMerchantProviderAPI(data: { userName: string }): AxiosPromise<any> {
  return request({
    url: '/payment/merchant/getSydByUserName',
    headers: {
      isToken: false,
      isEncrypt: false,
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    method: 'post',
    data: data
  });
}

// 第一步登录
export function tlogin(data: LoginData): AxiosPromise<WebSiteInfo> | any {
  const params = {
    ...data,
    clientId: data.clientId || clientId,
    grantType: data.grantType || 'password'
  };
  return request({
    url: '/auth/tlogin',
    headers: {
      isToken: false,
      isEncrypt: true,
      'Content-Type': 'application/json'
    },
    method: 'post',
    data: params
  });
}

export function loginSmsCode(data: { domain: string; id?: string | number }): AxiosPromise<WebSiteInfo> | any {
  return request({
    url: '/auth/loginSmsCode',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    method: 'post',
    data
  });
}

// 获取微信登陆二维码
export function loginWxPubCode(data: { domain: string; id: string; sydId: string }): AxiosPromise<any> {
  return request({
    url: '/auth/loginWxPubCode',
    headers: {
      isEncrypt: false,
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    method: 'post',
    data
  });
}

// 获取微信登录结果
export function loginWxPubResult(data: { id: string }): AxiosPromise<any> {
  return request({
    url: '/auth/loginWxPubResult',
    headers: {
      isEncrypt: false,
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    method: 'post',
    data
  });
}

// 查询商户默认资金账户
export function getDefaultAccount(): AxiosPromise<any> {
  return request({
    url: '/payment/merchantAccount/getDefaultAccount',
    method: 'get'
  });
}

// 获取商户相关的服务商
export function getSyds(): AxiosPromise<any> {
  return request({
    url: '/payment/merchant/getSyds',
    method: 'get'
  });
}

// 获取通道
export function getTunnel(params): AxiosPromise<any> {
  // return Promise.resolve({
  //   code: 200,
  //   data: [
  //     {
  //       value: '999',
  //       label: '通道1'
  //     }
  //   ]
  // });

  return request({
    url: '/payment/merchantAccount/getAccounts',
    method: 'get',
    params:params
  });
}

// 切换服务商
export function changeTunnel(params): AxiosPromise<any> {
  // return Promise.resolve({
  //   code: 200,
  //   msg: '切换成功'
  // });
  return request({
    url: '/payment/merchant/switchSyd',
    method: 'get',
    params:params
  });
}
